package com.wd.util;

import java.text.DecimalFormat;

public class DataUtil {

	public static double getFormatData(double data, int decimal) {
		/*
		 * DecimalFormat df = new DecimalFormat(); df. applyPattern(pattern);
		 */
		StringBuffer sb = null;
		if (decimal <= 0) {
			sb = new StringBuffer("0");
		} else {
			sb = new StringBuffer("0.");
		}
		for (int i = 0; i < decimal; i++) {
			sb.append("0");
		}
		DecimalFormat df = new DecimalFormat(sb.toString());
		Double formatData = new Double(df.format(data));
		return formatData;
	}
	
	public static String getFormat(double data, int decimal) {
		/*
		 * DecimalFormat df = new DecimalFormat(); df. applyPattern(pattern);
		 */
		StringBuffer sb = null;
		if (decimal <= 0) {
			sb = new StringBuffer("0");
		} else {
			sb = new StringBuffer("0.");
		}
		for (int i = 0; i < decimal; i++) {
			sb.append("0");
		}
		DecimalFormat df = new DecimalFormat(sb.toString());
		return df.format(data);
	}
	
	public static float getFormatData(float data, int decimal) {
		/*
		 * DecimalFormat df = new DecimalFormat(); df. applyPattern(pattern);
		 */
		StringBuffer sb = null;
		if (decimal <= 0) {
			sb = new StringBuffer("0");
		} else {
			sb = new StringBuffer("0.");
		}
		for (int i = 0; i < decimal; i++) {
			sb.append("0");
		}
		DecimalFormat df = new DecimalFormat(sb.toString());
		Float formatData = new Float(df.format(data));
		return formatData;
	}
	
	public static double getFormatData(String data){
		double d = Double.parseDouble(data);
		return getFormatData(d, 0);
	}
	
	public static double getFormatData(String data,int decimal){
		double d = Double.parseDouble(data);
		return getFormatData(d, decimal);
	}
	
	public static String cutSpilthZero(String str){
		if(str == null || "".equals(str)){
			return null;
		}
		char [] c = str.toCharArray();
		int count = 0;
		for(int j = (c.length-1) ; j >= 0; j--){
			if(!"0".equals(String.valueOf(c[j]))){
				break;
			}
			count++;
		}
		return str.substring(0,str.length() - count);
	}
	public static void main(String[] args) {
		double i = 0.000001;
		String str = getFormat(i, 8);
		System.out.println(str);
		System.out.println(cutSpilthZero(str));
	}
}
